﻿#include "VAOneXMLAdaptorMaterialIsosolid.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsMaterialsIsosolid.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QFile>
#include <iostream>



namespace IO
{
    bool VAOneXMLAdaptorMaterialIsosolid::adaptR()
    {
        // 返回操作是否成功
        return readMaterialIsosolid();
    }

    bool VAOneXMLAdaptorMaterialIsosolid::adaptW()
    {
        // 返回操作是否成功
        return writeMaterialIsosolid();
    }

    bool VAOneXMLAdaptorMaterialIsosolid::readMaterialIsosolid()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }


        auto nameNode = _node->first_node("Name")->first_node();

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "MaterialIsosolid name is null");
            return false;
        }

        Acoustics::FITKAcousticsMaterialsIsosolid* isosolid = dynamic_cast<Acoustics::FITKAcousticsMaterialsIsosolid*>(_dataObj);

        if(!isosolid)
        {
            return false;
        }

        //数据读取状态 默认为true
        bool b = true;
        // 名称字符串
        QString strName;
        strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            isosolid->setDataObjectName(strName);
        }

        // 读取各项属性
        // 每个属性可能存在缺省配置
        double dValue = readDoubleAttribute(_node, "density", &b);
        if(b) isosolid->setDensity(dValue);

        double smValue = readDoubleAttribute(_node, "shear_modulus", &b);
        if(b) isosolid->setShearModulus(smValue);

        double tmValue = readDoubleAttribute(_node, "tensile_modulus", &b);
        if(b) isosolid->setTensileModulus(tmValue);

        double v = tmValue / (2 * smValue) - 1;
        isosolid->setPoissonRatio(std::round(v * 10000.0) / 10000.0);

        // 读取备注
        auto note_node = _node->first_node("Note");
        if(note_node)
        {
            auto note = note_node->first_node();

            if (note != nullptr)
            {
                QString strNote = readStrNodeValue(note, &b);

                if(b)
                {
                    isosolid->setMaterialDescribe(strNote);
                }
            }
        }
        //读取成功
        return true;
    }

    bool VAOneXMLAdaptorMaterialIsosolid::writeMaterialIsosolid()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        Acoustics::FITKAcousticsMaterialsIsosolid* isosolid = dynamic_cast<Acoustics::FITKAcousticsMaterialsIsosolid*>(_dataObj);

        // 判断指针
        if (!isosolid)
        {
            return false;
        }

        // 写入各项属性
        writeAttribute(_node, "density", QString::number(isosolid->getDensity()));
        writeAttribute(_node, "tensile_modulus", QString::number(isosolid->getTensileModulus()));
        writeAttribute(_node, "shear_modulus", QString::number(isosolid->getShearModulus()));
        // 创建名称节点
        createNodeElement(_node, "Name", isosolid->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", isosolid->getMaterialDescribe());
        return true;
    }
}
